# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        # 先判断list1 or list2 是否为空 其中一方为空则直接输出另一方
        if not list1 and not list2:
            return list1
        elif list1 and not list2:
            return list1
        elif not list1 and list2:
            return list2
        else:
            # 二者都不为空则需要处理
            # 先把二者进行合同, 然后再排序
            list3: Optional[ListNode] = list1
            while list3.next:
                list3 = list3.next
            list3.next = list2
            # 进行排序
            while list3:

            return list1


if __name__ == '__main__':
    list1: Optional[ListNode] = ListNode(val=1)
    list1.next = ListNode(val=2)
    list1.next.next = ListNode(val=3)

    list2: Optional[ListNode] = ListNode(val=1)
    list2.next = ListNode(val=2)
    list2.next.next = ListNode(val=3)
    solution = Solution()
    list3 = solution.mergeTwoLists(list1=list1, list2=list2)
    while list3:
        print(list3.val)
        list3 = list3.next
